package com.quasar.hpatient.view.doctor;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.httpmodel.AnotherDataResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.HospitalInfodb;
import com.quasar.hpatient.model.litePalmodel.PositonInfodb;
import com.quasar.hpatient.model.medicalmodel.DoctorListBean;
import com.quasar.hpatient.model.medicalmodel.GetAmendVisitInfo;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.onInterface.OnDoctorDetailsListener;
import com.quasar.hpatient.presenter.DoctorDetailsPresenter;
import com.quasar.hpatient.utils.Spanny;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import chatui.ui.ChatActivity;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 医生详情1
 */
@EActivity(R.layout.activity_doctor_details)
public class DoctorDetailsActivity extends BaseActivity implements OnDoctorDetailsListener {

    //出诊信息
    private HashSet<String> joblist = new HashSet<String>();
    static HashMap<String, Integer> hashMap = new HashMap<>();
    String userCode=MainAppliaction.getInstance().getUserCode();
    private DoctorDetailsPresenter doctorDetailsPresenter;
    //科室
    List<PositonInfodb> positonInfodbList=MainAppliaction.getInstance().getPositonInfoList();
    //医院
    List<HospitalInfodb> hospitalInfodbList= MainAppliaction.getInstance().getHospitalInfodbList();
    //绑定控件
    static {
        hashMap.put("1-1", R.id.day_1_1);
        hashMap.put("1-2", R.id.day_1_2);
        hashMap.put("1-3", R.id.day_1_3);
        hashMap.put("2-1", R.id.day_2_1);
        hashMap.put("2-2", R.id.day_2_2);
        hashMap.put("2-3", R.id.day_2_3);
        hashMap.put("3-1", R.id.day_3_1);
        hashMap.put("3-2", R.id.day_3_2);
        hashMap.put("3-3", R.id.day_3_3);
        hashMap.put("4-1", R.id.day_4_1);
        hashMap.put("4-2", R.id.day_4_2);
        hashMap.put("4-3", R.id.day_4_3);
        hashMap.put("5-1", R.id.day_5_1);
        hashMap.put("5-2", R.id.day_5_2);
        hashMap.put("5-3", R.id.day_5_3);
        hashMap.put("6-1", R.id.day_6_1);
        hashMap.put("6-2", R.id.day_6_2);
        hashMap.put("6-3", R.id.day_6_3);
        hashMap.put("7-1", R.id.day_7_1);
        hashMap.put("7-2", R.id.day_7_2);
        hashMap.put("7-3", R.id.day_7_3);
    }

    //头像
    @ViewById(R.id.headimg)
    RoundedImageView headimg;
    //当前医生的对象
    @Extra
    DoctorListBean doctor;
    //擅长
    @ViewById(R.id.tv_good_at)
    TextView tv_good_at;
    //医院
    @ViewById(R.id.tv_hospital)
    TextView tv_hospital;
    //真实姓名
    @ViewById(R.id.username)
    TextView username;
    //职称
    @ViewById(R.id.tv_job_title)
    TextView tv_job_title;
    @ViewById(R.id.tv_communication)
    TextView tv_communication;
    @Extra
    String bomName;//传递过来的环信id
    @Extra
    String doctorId;


    @AfterViews
    void init() {
        ViseLog.d("enter");
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
    }

    @Override
    protected void initData() {

        doctorDetailsPresenter=new DoctorDetailsPresenter(this);

        if(doctor!=null){
            doctorxUser(doctor);
        }else if(bomName!=null){
            doctorDetailsPresenter.ChangeBomNameData(bomName);
            tv_communication.setVisibility(View.INVISIBLE);
        }else if(doctorId!=null){
            doctorDetailsPresenter.GetMyPatientInfoList(doctorId);
            tv_communication.setVisibility(View.INVISIBLE);
        }
    }



    //初始化医生信息
    public void doctorxUser(DoctorListBean doctorx){
        if(doctorx==null){
            finish();
           return;
        }
        //姓名
        if (doctorx.getRealName() != null && doctorx.getRealName().length() > 0) {
            username.setText(doctorx.getRealName());
        }
        //医院
        if (doctorx.getHospitalId() != 0) {
            int size=hospitalInfodbList.size();
            for(int i=0;i<size;i++){
                if(doctorx.getHospitalId()==hospitalInfodbList.get(i).getPrimaryid()){
                    tv_hospital.setText(hospitalInfodbList.get(i).getName());
                    break;
                }
            }
        }
        //擅长领域
        if (doctorx.getRemark() != null && doctorx.getRemark().length() > 0) {
            tv_good_at.setText(doctorx.getRemark());
        }
        //性别和职称
        String sex = null, jobTitle = null;
        if (doctorx.getSex() == 0) {
            sex = "女";
        } else {
            sex = "男";
        }
        //职称信息
        if (doctorx.getTitleId() != 0) {
            for(int i=0;i<positonInfodbList.size();i++){
                if(doctorx.getTitleId()==positonInfodbList.get(i).getPrimaryid()){
                    jobTitle=positonInfodbList.get(i).getName();
                    break;
                }
            }
        } else {
            jobTitle = "未知";
        }


        //头像
        if(doctorx.getCover()!=null && doctorx.getCover().length()>0){
            Glide.with(this).load(RetrofitConnection.PHOTOURL+doctorx.getCover()).into(headimg);
        }

        Spanny spanny = new Spanny();
        spanny.append(sex, new ForegroundColorSpan(color(R.color.textcolor)));
        spanny.append(" " + jobTitle, new ForegroundColorSpan(color(R.color.textcolor)));
        tv_job_title.setText(spanny);

        PostProvideInformation(doctorx);
    }


    private int color(int value) {
        return this.getResources().getColor(value);
    }

    //获取医生的坐诊信息
    public void PostProvideInformation(DoctorListBean doctorx){
        ViseLog.d("enter");
        RetrofitConnection.getRoutineApi()
                .ProvideInformationServer(userCode,doctorx.getId()+"")//医生Id
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GetAmendVisitInfo>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull GetAmendVisitInfo getAmendVisitInfo) {
                        showinfo_cd(getAmendVisitInfo);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        ViseLog.d("onError"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        ViseLog.d("onComplete");
                    }
                });
    }

    /**
     * 显示出诊信息
     *
     * @param getAmendVisitInfo
     */
    private void showinfo_cd(GetAmendVisitInfo getAmendVisitInfo) {
        List<String> ids = getAmendVisitInfo.getVisitInfo();
        if (ids != null) {
            Drawable drawable = getResources().getDrawable(R.mipmap.ico_c);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            //便利医生坐诊信息 到界面
            for (String key : hashMap.keySet()) {
                for (String datdti : ids) {
                    if (datdti.equals(key)) {
                        joblist.add(key);
                        TextView v = (TextView) this.findViewById(Integer.parseInt(hashMap.get(key) + ""));
                        v.setCompoundDrawables(drawable, null, null, null);
                        v.setText("出诊");

                    }
                }
            }
        }
    }
    //交流
    @Click(R.id.tv_communication)
    public void tv_communication(){
        if(doctor.getBmobUserName()!=null){
            this.startActivity(new Intent(this, ChatActivity.class).putExtra("userId",doctor.getBmobUserName()));
        }
    }

    @Override
    public void onSuccess(AnotherDataResult<DoctorListBean> data) {

        if(data!=null){
            ViseLog.d("医生的信心长度"+data.getList().size());
            if(data.getList()!=null && data.getList().size()>0){
                doctorxUser(data.getList().get(0));


            }

        }
    }

    @Override
    public void onError(String msg) {

        if(msg!=null){
            msg(msg);
        }
        ViseLog.d("查询医生信息"+msg);
    }

    @Override
    public void onRefreshUI() {

    }

    @Override
    public void onSavePhoto(DataStateResult dataStateResult) {

    }

    @Override
    public void OnselectIdDoctorx(AnotherDataResult<DoctorListBean> data) {
        if(data!=null){
            ViseLog.d("医生的信心长度"+data.getList().size());
            if(data.getList()!=null && data.getList().size()>0){
                doctorxUser(data.getList().get(0));
            }
        }
    }
}
